package gat.api.rest.background.tg;

import com.bitsun.core.common.persistence.Pager;
import com.bitsun.core.common.web.ResultDTO;
import gat.api.background.tg.AccountOrderApi;
import gat.api.config.annotation.CheckPermissions;
import gat.api.config.annotation.Log;
import gat.application.tg.AccountOrderService;
import gat.dto.req.tg.AccountDbReqDto;
import gat.dto.req.tg.AccountDbReqParams;
import gat.dto.req.tg.AccountOrderReqDto;
import gat.dto.req.tg.AccountOrderReqParams;
import gat.dto.res.tg.AccountDbResDto;
import gat.dto.res.tg.AccountOrderResDto;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 订单
 * @Author: yanshukeji-java
 * @Email: yanshukeji-java
 */
@Slf4j
@SuppressWarnings("unchecked")
@RestController
@RequestMapping("/accountOrder")
public class AccountOrderApiController implements AccountOrderApi {


    @Autowired
    private AccountOrderService accountOrderService;


    /**
     * 分页查询订单
     * @param params 参数params
     * @return
     */
    @Override
    @CheckPermissions(value = "accountOrder:queryPage")
    @GetMapping(produces = {"application/json"})
    public ResultDTO<Pager<AccountOrderResDto>> queryPage(AccountOrderReqParams params) {
        Pager<AccountOrderResDto> resDtoPager = accountOrderService.queryPage(params);
        return ResultDTO.ok(resDtoPager);
    }

    /**
     * 订单导出
     */
    @Log("导出订单列表")
    @CheckPermissions(value = "accountOrder:export")
    @PostMapping(value = "/export")
    public ResultDTO exportOrders(HttpServletResponse response, @RequestBody AccountOrderReqParams params) {
        accountOrderService.exportOrders(response,params);
        return ResultDTO.ok();
    }

    /**
     * 更新订单状态
     * @param id
     * @return
     */
    @CheckPermissions(value = "accountOrder:updateStatus")
    @PatchMapping(value = "/updateStatus/{id}", produces = {"application/json"})
    public ResultDTO<AccountOrderResDto> patchUpdateAccountOrderStatus(@ApiParam(value = "对象ID", required = true) @PathVariable("id") Long id) {
        AccountOrderReqDto accountOrderReqDto = new AccountOrderReqDto();
        accountOrderReqDto.setOrderStatus(1);
        accountOrderService.updateProps(id, accountOrderReqDto);
        AccountOrderResDto resDto = accountOrderService.selectOne(id);
        return ResultDTO.ok(resDto);
    }

    /**
     * 更新金额
     * @param id
     * @param reqDto
     * @return
     */
    @CheckPermissions(value = "accountOrder:updatePrice")
    @PatchMapping(value = "/updatePrice/{id}", produces = {"application/json"})
    public ResultDTO<AccountOrderResDto> patchUpdateAccountOrderPrice(@ApiParam(value = "对象ID", required = true) @PathVariable("id") Long id,
                                                           @ApiParam(value = "要修改的对象，对象属性有值的才更新,即null值不更新", required = true) @Valid @RequestBody AccountOrderReqDto reqDto) {
        AccountOrderReqDto accountOrderReqDto = new AccountOrderReqDto();
        accountOrderReqDto.setDiscount(reqDto.getDiscount());
        accountOrderService.updatePrice(id, accountOrderReqDto);
        AccountOrderResDto resDto = accountOrderService.selectOne(id);
        return ResultDTO.ok(resDto);
    }
}
